package javabase.demo03;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class ThreadBasics {
    public static void main(String[] args) {
        // 1. 继承Thread类
        MyThread thread1 = new MyThread();
        thread1.start();

        // 2. 实现Runnable接口
        Thread thread2 = new Thread(new MyRunnable());
        thread2.start();

        // 3. 实现Callable接口 + FutureTask
        FutureTask<Integer> futureTask = new FutureTask<>(new MyCallable());
        Thread thread3 = new Thread(futureTask);
        thread3.start();
        try {
            Integer result = futureTask.get();
            System.out.println("Callable返回值: " + result);
        }catch (Exception e){
            e.printStackTrace();
        }

        // 4. 使用Lambda表达式
        Thread thread4 = new Thread(() -> {
            System.out.println("Lambda线程: " + Thread.currentThread().getName());
        });
        thread4.start();

        // 5. 线程池方式（后面详细讲解）
    }
}

// 方式1: 继承Thread类
class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("继承Thread: " + Thread.currentThread().getName());
    }
}


// 方式2: 实现Runnable接口
class MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println("实现Runnable: " + Thread.currentThread().getName());
    }
}

// 方式3: 实现Callable接口
class MyCallable implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println("实现Callable: " + Thread.currentThread().getName());
        return 42;
    }
}